import java.math.BigInteger;
import java.util.Arrays;

public class t462 {
    //最少移动次数使数组元素相等2
    public static void main(String[] args) {
        int arr[] = {1,10,2,9};
        int min = minMoves2(arr);
        System.out.println(min);
    }
    //特殊数字为中位数
    public static int minMoves2(int[] nums) {
        int num =0;
        int n = nums.length;
        Arrays.sort(nums);
        int mid = nums[n/2];
        for(int i =0;i<n;i++){
            num+=Math.abs(mid-nums[i]);
        }
        return num;
    }
    //暴力枚举500ms
    /*public static int minMoves2(int[] nums) {
        int min =Integer.MAX_VALUE;
        int n = nums.length;
        for(int i =0;i<n;i++){
            long tmp = 0;
            for(int j =0;j<n;j++){
                tmp += Math.abs(nums[i]-nums[j]);
            }
            min = (int)Math.min((long)min,tmp);
        }
        return min;
    }*/
}
